package com.moon.game.client;

import org.apache.log4j.Logger;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

import com.moon.game.message.ObjMessage;
import com.moon.game.server.DataSession;
import com.moon.game.server.NetServer;

class UdpClientHandler extends SimpleChannelUpstreamHandler {

    private static final Logger log = Logger.getLogger(UdpClientHandler.class);

    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    	ObjMessage response = (ObjMessage) e.getMessage();
    	DataSession dataSession = NetServer.getDataSession(response.getSid());
    	if(dataSession!=null){
			// 添加应答数据
    		NetServer.addResponseMsg(response);
			// 通知输出页面
	        synchronized(dataSession.getChannel()){
	        	dataSession.getChannel().notify();
	        }
	        // 移除Session
	        NetServer.removeDataSession(response.getSid());
		}
    }

    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        log.error("server data exception,session", e.getCause());
    }

}